﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Vehicle.Web.Pages.Transit
{
    /// <summary>
    /// Copyright (C) M-Tear团队
    /// 文 件 名：Transit_Main.aspx.cs
    /// 版    本：v1.00.0000
    /// 创建标识：2009-08-31   Created by 杨光远 V1.00.0000
    /// 功能说明：运力综合查询页面类
    /// 注意事项：无
    /// 
    /// 更新记录：
    /// 
    /// </summary>
    public partial class Transit_Main : Component.PageBase
    {
        #region ---基本变量实例化---
        Vehicle.BLL.Vehicles b_Vechicles = new Vehicle.BLL.Vehicles();
        Vehicle.BLL.Motorcade b_Motorcade = new Vehicle.BLL.Motorcade();//定义逻辑层变量
        Model.VehiclesInfo m_VehiclesInfo = new Vehicle.Model.VehiclesInfo();//实例化实体层
        public string onshow;//定义没有数据时页面的显示内容
        #endregion

        #region---基本操作---
        /// <summary>
        /// 页面加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                MotorcadeDataBind();//绑定下拉车辆
                RepeaterDataBind();
            }

        }
        /// <summary>
        /// 初始绑定车队
        /// </summary>
        public void MotorcadeDataBind()
        {
            b_Motorcade.DropDownListBind(Drop_Select);//绑定车队
            Drop_Select.Items.FindByValue("0").Text = "全部";
            Drop_Select.SelectedValue = "0";
        }

        /// <summary>
        /// 绑定页面Repeater数据
        /// </summary>
        protected void RepeaterDataBind()
        {
            if (rbt_Motorcade.Checked)//选择车队
            {
                ReptShow.DataSource = b_Motorcade.GetModel(new Vehicle.Model.ComponentsInfo("Vehicles", Drop_Select.SelectedValue == "0" ? "0" : "M_ID", Drop_Select.SelectedValue == "0" ? "0" : Drop_Select.SelectedValue, drop_M_transit.SelectedValue == "2" ? "0" : "V_Transit", drop_M_transit.SelectedValue == "2" ? "0" : drop_M_transit.SelectedValue, "V_IsDelete", "1", "V_ID")).DefaultView;
                ReptShow.DataBind();
            }
            else
            {
                ReptShow.DataSource = b_Motorcade.GetModel(new Vehicle.Model.ComponentsInfo("Vehicles", "V_LicenseNum", txt_V_LicenseNum.Text.Trim(), "1", "1", "V_IsDelete", "1", "V_ID")).DefaultView;
                ReptShow.DataBind();
            }
            if (ReptShow.Items.Count == 0)//判断没有数据时页面的显示内容
            {
                onshow = "当前页面无数据";
            }
            else
            {
                onshow = "";
            }
        }
        /// <summary>
        /// 根据车队查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Drop_Select_SelectedIndexChanged(object sender, EventArgs e)
        {
            RepeaterDataBind();
        }
        /// <summary>
        /// 根据任务查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void drop_M_transit_SelectedIndexChanged(object sender, EventArgs e)
        {
            RepeaterDataBind();
        }


        /// <summary>
        /// 根据车牌号查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Select_Click(object sender, EventArgs e)
        {
            RepeaterDataBind();
        }

        /// <summary>
        /// 导出EXCEL文件
        /// </summary>
        /// <param name="dtData"></param>
        public void DataTable2Excel()
        {
            if (ReptShow.Items.Count > 0)
            {

                // 当前对话
                System.Web.HttpContext curContext = System.Web.HttpContext.Current;
                // IO用于导出并返回excel文件
                System.IO.StringWriter strWriter = null;
                System.Web.UI.HtmlTextWriter htmlWriter = null;
                curContext.Response.ContentType = "application/vnd.ms-excel";
                curContext.Response.ContentEncoding = System.Text.Encoding.Default;
                curContext.Response.Charset = "";

                strWriter = new System.IO.StringWriter();
                htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
                // 返回客户端
                ReptShow.RenderControl(htmlWriter);
                curContext.Response.Write(strWriter.ToString());
                curContext.Response.End();
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(UPanel, this.GetType(), "click", "alert('当前页面无数据,无法导出!')", true);
            }
        }
        /// <summary>
        /// 导出EXCEL文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Excel_Click(object sender, EventArgs e)
        {
            DataTable2Excel();
        }
        /// <summary>
        /// Repeater绑定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ReptShow_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {

            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {

                Label lab_M_Transit = (Label)e.Item.FindControl("lab_V_Transit");//更改是否承运任务为中文显示
                if (lab_M_Transit.Text == "1")
                {
                    lab_M_Transit.Text = "是";
                }
                else
                {
                    lab_M_Transit.Text = "否";
                }
                Label lab_M_ID = (Label)e.Item.FindControl("lab_M_ID");//更改所属车队为中文显示
                try
                {
                    lab_M_ID.Text = b_Vechicles.GetModel(new Model.ComponentsInfo("Motorcade", "M_ID", lab_M_ID.Text, "M_ID")).Rows[0]["M_Name"].ToString();//绑定车队名称
                }
                catch
                {
                    lab_M_ID.Text = "无";
                }

                Label lab_P_ID = (Label)e.Item.FindControl("lab_P_ID");//更改所属驾驶员为中文显示
                try
                {
                    lab_P_ID.Text = b_Vechicles.GetModel(new Model.ComponentsInfo("Pilot", "P_ID", lab_P_ID.Text, "P_ID")).Rows[0]["P_Name"].ToString();//绑定驾驶员姓名
                }
                catch
                {
                    lab_P_ID.Text = "无";
                }
            }
        }
        #endregion
    }
}
